package com.jackeysun.widget.media

import android.content.Context
import android.media.AudioManager

/**
 *   Created by sunxunchao on 2022/1/4.
 */
class AudioPlayer(context: Context) {

    val mSimplePlayer: SimplePlayer by lazy {
        SimplePlayer()
    }
    private var mAudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

    var speedLevel: Int = 0
    var speeds = mutableListOf(1.0f)
    var path: String? = null
        set(value) {
            field = value
            initSource()
        }
    var startTime: Int = 0
    var loadResourceCompleteToPlay = false
    var durationHint: Int = AudioManager.AUDIOFOCUS_GAIN_TRANSIENT

    var onPlayerStatusChangeListener: OnPlayerStatusChangeListener? = null

    fun getSpeed(): Float =
        when {
            speedLevel < 0 || speedLevel >= speeds.size -> {
                1.0f
            }
            else -> {
                speeds[speedLevel]
            }
        }


    fun initSource() {
        mSimplePlayer.reset()
        mSimplePlayer.setSource(path ?: return)
        mSimplePlayer.onPlayerStatusChangeListener =
            object : SimplePlayer.OnPlayerStatusChangeListener {
                override fun onStatusChanged(status: SimplePlayer.Status) {
                    onPlayerStatusChangeListener?.onStatusChanged(status)
                    when (status) {
                        SimplePlayer.Status.PREPARED -> {
                            var start = mSimplePlayer.getCurrentPosition()
                            val end = mSimplePlayer.getDuration()
                            start = checkSeekTo(startTime, IntRange(start, end))
                            if (start > 0) {
                                mSimplePlayer.seekTo(start)
                            }
                            if (loadResourceCompleteToPlay) {
                                mSimplePlayer.start()
                            }
                        }
                        SimplePlayer.Status.PLAYING -> {
                            mSimplePlayer.requestAudioFocusSuccess(
                                mAudioManager,
                                durationHint
                            )
                        }
                        SimplePlayer.Status.COMPLETE,
                        SimplePlayer.Status.PAUSE,
                        SimplePlayer.Status.STOP,
                        SimplePlayer.Status.ERROR,
                        SimplePlayer.Status.RELEASE -> {
                            mSimplePlayer.abandonAudioFocusSuccess(mAudioManager)
                        }
                    }
                }
            }
    }

    private fun checkSeekTo(msec: Int, range: IntRange): Int =
        when {
            msec >= range.first && msec <= range.last -> msec
            else -> 0
        }

    fun getStatus(): SimplePlayer.Status = mSimplePlayer.status

    fun setSpeed(speed: Float): Float =
        mSimplePlayer.setSpeed(speed)

    fun seekTo(msec: Int) {
        mSimplePlayer.seekTo(msec)
    }

    fun onStart() {
        mSimplePlayer.start()
    }

    fun onPause() {
        mSimplePlayer.pause()
    }

    fun onStop() {
        mSimplePlayer.stop()
    }

    fun release() {
        mSimplePlayer.release()
    }

    fun getCurrentPosition(): Int =
        mSimplePlayer.getCurrentPosition()

    fun getDuration(): Int =
        mSimplePlayer.getDuration()

    interface OnPlayerStatusChangeListener {
        fun onStatusChanged(status: SimplePlayer.Status)
    }
}
